package com.lmpop.frame.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Config
  implements Serializable
{
  private Properties props;
  private String propertiesName = "config.properties";

  public Config() {
    this.props = new Properties();
    init(propertiesName);
  }

  public Config(String properName) {
    this.props = new Properties();
    this.props =  InitProject.initProperties(properName);
    }

  public void init(String properName) {
	this.props = InitProject.initProperties(properName);
  }


  public String getString(String key)
  {
    try
    {
      String value = "";
      if (this.props.getProperty(key) != null) {
        value = this.props.getProperty(key);
      }
      return value;
    } catch (Exception e) {
      e.printStackTrace();
    }return "";
  }

  public Set<String> getPropNames()
  {
    Set propNameSet = this.props.stringPropertyNames();

    return propNameSet;
  }

  public void setString(String key, String value) {
    if (!key.equals(""))
      this.props.setProperty(key, value);
  }

  public void setString(Map map)
  {
    Set entrySet = null;
    if ((map != null) && (map.size() > 0)) {
      entrySet = map.entrySet();
      Iterator iterator = entrySet.iterator();
      while (iterator.hasNext()) {
        Map.Entry entry = (Map.Entry)iterator.next();
        String key = (String)entry.getKey();
        String value = (String)entry.getValue();
        this.props.setProperty(key, value);
      }
    }
  }

  public void saveConfig(String comments)
  {
    String filePath = InitProject.classPath() + this.propertiesName;
    OutputStream out = null;
    try
    {
      out = new FileOutputStream(filePath);
      this.props.store(out, comments);
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        out.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args)
  {
    System.out.println(System.getProperty("file.separator"));
  }
}